package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.slg.android.manyanime.CartoonCategoryDetailsActivity;
import com.slg.android.manyanime.CartoonDetailActivity;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.CartoonCategory;
import com.slg.android.manyanime.utils.DataStore;

import java.util.List;

/**
 * Created by Thinkpad on 2015/3/12.
 */
public class CartoonGroupInnerAdapter extends BaseAdapter {

    public CartoonGroupInnerAdapter(Context context, List<CartoonCategory> data) {
        this.context = context;
        this.data = data;
    }

    private Context context;

    private List<CartoonCategory> data;

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.item_cartoon_category_grid, parent, false);
        }

        ViewHolder viewHolder = (ViewHolder) view.getTag();
        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            viewHolder.gridImg = (ImageView) view.findViewById(R.id.item_cartoon_category_grid_img);
            viewHolder.textView = (TextView) view.findViewById(R.id.item_cartoon_category_grid_name);
            view.setTag(viewHolder);
        }

        BitmapUtils bitmapUtils = DataStore.getBitmapUtils();
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.cartoon_hot_loading);
        bitmapUtils.configDefaultLoadingImage(R.drawable.cartoon_hot_loading);
        bitmapUtils.display(viewHolder.gridImg, data.get(position).getPic());

        final String cateName=data.get(position).getCateName();
        viewHolder.textView.setText(cateName);
        final int  cateId=data.get(position).getId();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, CartoonCategoryDetailsActivity.class);
                intent.putExtra("cateId",cateId);
                intent.putExtra("cateName",cateName);
                context.startActivity(intent);

            }
        });

        return view;
    }

    public class ViewHolder {
        public ImageView gridImg;
        public TextView textView;

    }
}
